package clientDemo.service.impl;

import clientDemo.entity.MissionWork;
import clientDemo.entity.MissionWorkAction;
import clientDemo.entity.RuntimeParameter;
import clientDemo.httpUtil.HttpMethod;
import clientDemo.httpUtil.HttpRequest;
import clientDemo.httpUtil.HttpResponse;
import clientDemo.service.MissionWorkService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.List;

public class MissionWorkServiceImpl implements MissionWorkService {

	@Override
	public MissionWork create(String missionId) {
		HttpRequest request = new HttpRequest();
		request.method(HttpMethod.POST).host("127.0.0.1").port(8080).path("/api/v2/missionWorks/" + missionId);
		HttpResponse response = request.send();
		int status = response.statusCode();
		if (status == 200) {
			return JSON.parseObject(response.body(), MissionWork.class);
		}
		return null;
	}

	@Override
	public MissionWork get(String id) {
		HttpRequest request = HttpRequest.get("http://127.0.0.1:8080/api/v2/missionWorks/" + id);
		HttpResponse response = request.send();
		int status = response.statusCode();
		if (status == 200) {
			return JSON.parseObject(response.body(), MissionWork.class);
		}
		return null;

	}

	@Override
	public void pause(String id) {
		HttpRequest request = HttpRequest.post("http://127.0.0.1:8080/api/v2/missionWorks/" + id + "/controls/pause");
		HttpResponse response = request.send();
		response.statusCode();
	}

	@Override
	public void resume(String id) {
		HttpRequest request = HttpRequest.post("http://127.0.0.1:8080/api/v2/missionWorks/" + id + "/controls/resume");
		HttpResponse response = request.send();
		response.statusCode();
	}

	@Override
	public void stop(String id) {
		HttpRequest request = HttpRequest.post("http://127.0.0.1:8080/api/v2/missionWorks/" + id + "/controls/stop");
		HttpResponse response = request.send();
		response.statusCode();
	}

	@Override
	public List<RuntimeParameter> getRuntimeParameters(String id) {
		HttpRequest request = HttpRequest
				.get("http://127.0.0.1:8080/api/v2/missionWorks/" + id + "/emptyRuntimeParameters");
		HttpResponse response = request.send();
		int status = response.statusCode();
		if (status == 200) {
			return JSONObject.parseArray(response.body(), RuntimeParameter.class);
		}
		return null;
	}

	@Override
	public List<MissionWorkAction> getMissionWorkActions(String id) {
		HttpRequest request = HttpRequest
				.get("http://127.0.0.1:8080/api/v2/missionWorks/" + id + "/missionWorkActions");
		HttpResponse response = request.send();
		int status = response.statusCode();
		if (status == 200) {
			return JSONObject.parseArray(response.body(), MissionWorkAction.class);
		}
		return null;

	}
}
